package cn.laixueit;

import cn.hutool.core.collection.AvgPartition;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.net.URLDecoder;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.extra.spring.SpringUtil;
import cn.laixueit.po.TbBook;
import cn.laixueit.po.TbBookChapter;
import cn.laixueit.po.table.TbBookChapterTableDef;
import cn.laixueit.po.table.TbBookTableDef;
import cn.laixueit.resource.blog.BlogApp;
import cn.laixueit.resource.blog.mapper.TbBookMapper;
import cn.laixueit.resource.blog.service.TbBookService;
import cn.laixueit.vo.BookVo;
import com.jayway.jsonpath.internal.function.numeric.Max;
import com.mybatisflex.core.query.QueryChain;
import com.mybatisflex.core.query.QueryColumn;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import org.jasypt.encryption.StringEncryptor;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.Charset;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author duxiang
 * @date 2024/2/5 9:47
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = BlogApp.class)
public class Demo {

    @Resource
    private TbBookService bookService;

    @Test
    public void t2(){
        System.out.println("bookService.list() = " + bookService.list());
    }

    @Resource
    private StringEncryptor stringEncryptor;

    @Test
    public  void t3(){
        String root = stringEncryptor.encrypt("123456");
        System.out.println("root = " + root);
    }

    @Test
    public  void t4(){
        String yyyyMMdd = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
        System.out.println(yyyyMMdd);
    }

    @Test
    public  void t5(){
        bookService.remove("1",true);
    }

    @Test
    public  void t6(){
        List<TbBook> list = bookService.list(QueryChain.of(TbBook.class).eq(TbBook::getCategoryName, "java"));
        System.out.println(list);
    }
    @Test
    public  void t7(){
        String yyyyMMdd = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
        String s = String.format(yyyyMMdd + "%03d", 1);
        System.out.println("s = " + s);
    }

    @Test
    public  void t8(){
        BookVo bookVo = bookService.findById("1");
        System.out.println("bookVo = " + bookVo);
    }

    @Test
    public  void t9(){
        String profile = SpringUtil.getActiveProfile();
        System.out.println(profile);
    }

    @Resource
    private TbBookMapper bookMapper;

    @Test
    public  void t10(){
        List<TbBook> tbBooks = bookMapper.selectAllWithRelations();
        System.out.println("tbBooks = " + tbBooks);
    }

    @Test
    public  void t11(){
        BookVo tbBook = bookMapper.selectOneWithRelationsByIdAs("1",BookVo.class);
        System.out.println("tbBook = " + tbBook);
    }

    @Test
    public  void t12(){
        QueryWrapper wrapper = QueryWrapper.create().select(TbBookTableDef.TB_BOOK.NAME,TbBookTableDef.TB_BOOK.CREATE_TIME)
                .select(TbBookChapter::getName).from(TbBookTableDef.TB_BOOK).leftJoin(TbBookChapterTableDef.TB_BOOK_CHAPTER)
                .on(TbBookTableDef.TB_BOOK.ID.eq(TbBookChapterTableDef.TB_BOOK_CHAPTER.BOOK_ID))
                .where(TbBookTableDef.TB_BOOK.ID.eq("1"));
        List<TbBook> tbBooks = bookMapper.selectListByQuery(wrapper);
        tbBooks.forEach(System.out::println);
    }

    @Test
    public  void t13(){
        BookVo bookVo = bookService.findBookAndChapters("1");
        System.out.println("bookVo = " + bookVo);
    }

    @Test
    public  void t14(){
        List<TbBookChapter> chapters = bookService.listChapter("1");
        System.out.println("chapters = " + chapters);
    }

    @Test
    public  void t15(){
        String s = Assert.notBlank("1");
        System.out.println("s = " + s);
    }

    @Test
    public  void t16(){
        String s = "%E5%85%B6%E5%AE%83";
        String s1 = URLDecoder.decode(s, Charset.defaultCharset());
        System.out.println("s1 = " + s1);
    }

    @Test
    public  void t17() throws IOException {
        byte[] md5 = DigestUtil.md5(new File("D:\\information\\html模板\\个人博客模板\\images\\image_2copy.jpg"));
        System.out.println("md5 = " + Arrays.toString(md5));
        String md5str = new String(md5,Charset.defaultCharset());
        System.out.println("md5str = " + md5str);
        byte[] md5Digest = DigestUtils.md5Digest(new FileInputStream(new File("D:\\information\\html模板\\个人博客模板\\images\\image_2copy.jpg")));
        System.out.println("md5Digest = " + Arrays.toString(md5Digest));
    }

    @Test
    public  void t18() throws IOException {
        byte[] md5 = DigestUtil.md5(new File("D:\\information\\html模板\\个人博客模板\\images\\image_2copy.jpg"));
        String join = StrUtil.join("", md5);
        System.out.println("join = " + join);
    }

    @Test
    public  void t19() throws IOException {
        List<HashMap> maps = bookService.listAs(QueryWrapper.create().select(TbBookTableDef.TB_BOOK.CATEGORY_NAME.as("categoryName")).select("count(1) as count").groupBy(TbBook::getCategoryName).from(TbBookTableDef.TB_BOOK), HashMap.class);
        System.out.println("maps = " + maps);
    }

    @Test
    public  void t20() throws IOException {
        List<TbBook> books = bookService.list(QueryWrapper.create().select(TbBookTableDef.TB_BOOK.CREATE_TIME).groupBy(TbBook::getCreateTime).orderBy(TbBook::getCreateTime,false).from(TbBookTableDef.TB_BOOK));
        Set<Integer> years = books.stream().map(TbBook::getCreateTime).map(x -> {
            LocalDate localDate = x.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
            return localDate.getYear();
        }).collect(Collectors.toSet());
        HashMap<String, Set<String>> map = new HashMap<>();
        years.stream().forEach(x->{
            map.put(x.toString(),new HashSet<>());
            books.stream().forEach(y->{
                LocalDate localDate = y.getCreateTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
                if (StrUtil.equals(x.toString(),localDate.getYear()+"")) {
                    map.get(x.toString()).add(localDate.getMonthValue()+"");
                }
            });
            Arrays.sort(map.get(x.toString()).toArray());
        });
        System.out.println("map = " + map);
    }

    @Test
    public  void t21() throws IOException {
        LocalDate localDate = LocalDate.of(2023, 1, 1);
        localDate = localDate.plusMonths(1);
        LocalDate localDate2 = localDate.plusMonths(12-localDate.getMonthValue()).plusDays(31);
        System.out.println("localDate = " + localDate);
        System.out.println("localDate2 = " + localDate2);
    }

    @Test
    public  void t22() throws IOException {
        DateTime dateTime = DateUtil.beginOfMonth(new Date());
        DateTime dateTime2 = DateUtil.endOfMonth(new Date());
        System.out.println("dateTime = " + dateTime);
        System.out.println("dateTime2 = " + dateTime2);
    }

    @Test
    public  void t23() throws IOException {
        BookVo bookVo = new BookVo();
//        bookVo.setYear("2021");
//        bookVo.setMonth("2");
        bookService.bookPage(1,10,bookVo);
    }

    @Test
    public  void t24() throws IOException {
        List<TbBook> tbBooks = bookService.randHotBook();
    }

}

